{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "8b3217b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "account_id_str=\"\"\"\n",
    "case\n",
    "when dsp_name='vungle' then 'DA0000000028'\n",
    "when dsp_name='unity' then 'DA0000000029'\n",
    "when dsp_name='admob' then 'DA0000000025'\n",
    "when dsp_name='adview' then 'DA0000000015'\n",
    "when dsp_name='apptv' then 'DA0000000013'\n",
    "when dsp_name='criteo' then 'DA0000000014'\n",
    "when dsp_name='facebook' then 'DA0000000024'\n",
    "when dsp_name='inmobi' then 'DA0000000004'\n",
    "when dsp_name='cpi_mintegral' then 'RA0000000330'\n",
    "when dsp_name='cpi_mobvista' then 'RA0000000330'\n",
    "when dsp_name='cpm_mintegral' then 'DA0000000017'\n",
    "when dsp_name='cpm_mobvista' then 'DA0000000017'\n",
    "when dsp_name='mopub' then 'DA0000000023'\n",
    "when dsp_name='mytarget' then 'DA0000000016'\n",
    "when dsp_name='pubnative' then 'DA0000000005'\n",
    "when dsp_name='smaato' then 'DA0000000006'\n",
    "when dsp_name='smadex' then 'DA0000000012'\n",
    "when dsp_name='startapp' then 'DA0000000009'\n",
    "when dsp_name='telecoming' then 'DA0000000010'\n",
    "when dsp_name='cpi_xapads' then 'DA0000000065'\n",
    "when dsp_name='cpm_xapads' then 'DA0000000007'\n",
    "when dsp_name='flymobi' then 'DA0000000064'\n",
    "when dsp_name='yandex' then 'DA0000000011'\n",
    "when dsp_name='yeahmobi_online' then 'DA0000000021'\n",
    "when dsp_name='yeahmobi' then 'DA0000000001'\n",
    "when dsp_name='appnext' then 'RA0000000328'\n",
    "when dsp_name='affise' then 'DA0000000026'\n",
    "when dsp_name='affle' then 'DA0000000022'\n",
    "when dsp_name='mobupps' then 'DA0000000033'\n",
    "when dsp_name='mobisummer' then 'DA0000000034'\n",
    "when dsp_name='Apple' then 'DA0000000050'\n",
    "when dsp_name='adsense' then 'DA0000000063'\n",
    "when dsp_name='affinity' then 'DA0000000041'\n",
    "when dsp_name='flatads' then 'DA0000000042'\n",
    "when dsp_name='nativex' then 'DA0000000055'\n",
    "when dsp_name='xiaomi' then 'DA0000000057'\n",
    "when dsp_name='xinzhan' then 'DA0000000054'\n",
    "when dsp_name='avow' then 'RA0000000528'\n",
    "when dsp_name='mobavenue' then 'DA0000000045'\n",
    "when dsp_name='vfine' then 'DA0000000047'\n",
    "when dsp_name='cchange' then 'DA0000000058'\n",
    "when dsp_name='applovin' then 'DA0000000076'\n",
    "when dsp_name='pangle' then 'DA0000000071'\n",
    "when dsp_name='bigo' then 'DA0000000075'\n",
    "when dsp_name='RTBhouse' then 'DA0000000089'\n",
    "else '' end\n",
    "\"\"\".strip()\n",
    "\n",
    "account_name_str=\"\"\"\n",
    "case\n",
    "when dsp_name='admob' then 'Admob'\n",
    "when dsp_name='unity' then 'Unity'\n",
    "when dsp_name='vungle' then 'Vungle'\n",
    "when dsp_name='adview' then 'SHANGYOUJIA TECHNOLOGY (HK) LIMITED'\n",
    "when dsp_name='apptv' then 'appTV.LTD'\n",
    "when dsp_name='criteo' then 'Criteo Singapore Pte.Ltd'\n",
    "when dsp_name='facebook' then 'Facebook,Inc.'\n",
    "when dsp_name='inmobi' then 'InMobi PTE Limited'\n",
    "when dsp_name='cpi_mobvista' then 'Mobvista'\n",
    "when dsp_name='cpi_mintegral' then 'Mobvista'\n",
    "when dsp_name='cpm_mobvista' then '聚移国际有限公司'\n",
    "when dsp_name='cpm_mintegral' then '聚移国际有限公司'\n",
    "when dsp_name='mopub' then 'TwitterInc.'\n",
    "when dsp_name='mytarget' then 'My.Com B.V.'\n",
    "when dsp_name='pubnative' then 'Pubnative GmbH'\n",
    "when dsp_name='smaato' then 'Smaato Inc.'\n",
    "when dsp_name='smadex' then 'SMADEX S.L.'\n",
    "when dsp_name='startapp' then 'StartAppInc.'\n",
    "when dsp_name='telecoming' then 'Telecoming, S.A.'\n",
    "when dsp_name='cpm_xapads' then 'Xapads Media Pvt Ltd'\n",
    "when dsp_name='cpi_xapads' then 'Xapads-DSP_CPI'\n",
    "when dsp_name='flymobi' then 'Flymobi-DSP_CPI'\n",
    "when dsp_name='yandex' then 'Yandex Europe AG'\n",
    "when dsp_name='yeahmobi_online' then 'Click Tech Limited'\n",
    "when dsp_name='yeahmobi' then 'Yeahmobi-Solomon'\n",
    "when dsp_name='appnext' then 'Appnext'\n",
    "when dsp_name='affise' then 'Affise'\n",
    "when dsp_name='affle' then 'Affle'\n",
    "when dsp_name='mobupps' then 'Mobupps_DSP_CPI'\n",
    "when dsp_name='mobisummer' then 'Mobisummer_DSP_CPI'\n",
    "when dsp_name='Apple' then 'Apple Store'\n",
    "when dsp_name='adsense' then 'H5-Adsense'\n",
    "when dsp_name='affinity' then 'Affinity-DSP_CPI'\n",
    "when dsp_name='flatads' then 'Flatads-DSP_CPI'\n",
    "when dsp_name='nativex' then 'NativeX-DSP_CPI'\n",
    "when dsp_name='xiaomi' then 'Xiaomi-DSP_CPI'\n",
    "when dsp_name='xinzhan' then 'xinzhan-DSP_CPI'\n",
    "when dsp_name='avow' then 'AVOW-DSP_CPI'\n",
    "when dsp_name='mobavenue' then 'MobAvenue-DSP_CPI'\n",
    "when dsp_name='vfine' then 'Vfine-DSP_CPI'\n",
    "when dsp_name='cchange' then 'Cchange-DSP_CPI'\n",
    "when dsp_name='applovin' then 'AppLovin'\n",
    "when dsp_name='pangle' then 'BYTEDANCE PTE. LTD'\n",
    "when dsp_name='bigo' then 'BIGO TECHNOLOGY PTE.LTD'\n",
    "when dsp_name='RTBhouse' then 'RTB MARKETING & TECH SERVICES LIMITED'\n",
    "else '' end\n",
    "\"\"\".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "82ec70bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def line_parser(line=\"when dsp_name='vungle' then 'DA0000000028'\"):\n",
    "    return line.replace('\\'','').split('dsp_name=')[1].split(' then ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "f8be4dbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "def update(case_when_str,update_key='account_id',result_dict={}):\n",
    "    dsp_dict={'account_id': '', 'account_name': '', 'so_header': 0,\n",
    "                 'so_line': 0}\n",
    "    for line in case_when_str.split('\\n'):\n",
    "        if line.__contains__('when') and line.__contains__('then'):\n",
    "            line=' '.join(re.split('\\s+',line))\n",
    "            cols=line_parser(line)\n",
    "            dsp_name=cols[0]\n",
    "            acc_info=cols[1]\n",
    "            if result_dict.get(dsp_name)  is not None:\n",
    "                dsp_dict=result_dict.get(dsp_name)\n",
    "            dsp_dict[update_key]=acc_info\n",
    "            result_dict[dsp_name]=dsp_dict.copy()\n",
    "        else:\n",
    "            pass\n",
    "    return result_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "59836086",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "a_dict=update(case_when_str=account_id_str,update_key='account_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "9111d305",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dict=update(case_when_str=account_name_str,update_key='account_name',result_dict=a_dict)\n",
    "res_dict=a_dict.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "c826e497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'account_id': 'DA0000000045',\n",
       " 'account_name': 'MobAvenue-DSP_CPI',\n",
       " 'so_header': 0,\n",
       " 'so_line': 0}"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_dict.get('mobavenue')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "398b6369",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('/Users/msxr/a','r') as fr:\n",
    "    for line in fr.readlines():\n",
    "        dsp_name,account_id,so_header,so_line,account_name=line.replace('\\n','').split('\\t')\n",
    "        res_dict[dsp_name]={'account_id': account_id,\n",
    " 'account_name': account_name,\n",
    " 'so_header': int(so_header),\n",
    " 'so_line': int(so_line)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "c2da8502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'account_id': 'DA0000000045',\n",
       " 'account_name': 'MobAvenue Pte Ltd.',\n",
       " 'so_header': 10010033,\n",
       " 'so_line': 15069}"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_dict.get('mobavenue')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "3718e9fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adview DA0000000015\n",
      "apptv DA0000000013\n",
      "facebook DA0000000024\n",
      "cpi_mobvista RA0000000330\n",
      "cpm_mobvista DA0000000017\n",
      "mopub DA0000000023\n",
      "startapp DA0000000009\n",
      "cpm_xapads DA0000000007\n",
      "yandex DA0000000011\n",
      "affise DA0000000026\n",
      "Apple DA0000000050\n",
      "xinzhan DA0000000054\n",
      "bigo DA0000000075\n",
      "RTBhouse DA0000000089\n"
     ]
    }
   ],
   "source": [
    "for i in a_dict.keys():\n",
    "    try:\n",
    "        if a_dict.get(i)['account_id'] != b_dict.get(i)['account_id']:\n",
    "            print('{},{}\\t{}'.format(i,b_dict.get(i)['account_id'],a_dict.get(i)['account_id']))\n",
    "    except Exception:\n",
    "        print(i,a_dict.get(i)['account_id'])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "e9494e1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'vungle': {'account_id': 'DA0000000028',\n",
       "  'account_name': 'Vungle Inc.',\n",
       "  'so_header': 10010064,\n",
       "  'so_line': 15100},\n",
       " 'unity': {'account_id': 'DA0000000029',\n",
       "  'account_name': 'unity',\n",
       "  'so_header': 10010063,\n",
       "  'so_line': 15099},\n",
       " 'admob': {'account_id': 'DA0000000025',\n",
       "  'account_name': 'Admob',\n",
       "  'so_header': 10010061,\n",
       "  'so_line': 15097},\n",
       " 'adview': {'account_id': 'DA0000000015',\n",
       "  'account_name': 'SHANGYOUJIA TECHNOLOGY (HK) LIMITED',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'apptv': {'account_id': 'DA0000000013',\n",
       "  'account_name': 'appTV.LTD',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'criteo': {'account_id': 'DA0000000014',\n",
       "  'account_name': 'Criteo Technology.',\n",
       "  'so_header': 10010069,\n",
       "  'so_line': 15105},\n",
       " 'facebook': {'account_id': 'DA0000000024',\n",
       "  'account_name': 'Facebook,Inc.',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'inmobi': {'account_id': 'DA0000000004',\n",
       "  'account_name': 'InMobi Pte. Ltd.',\n",
       "  'so_header': 10010030,\n",
       "  'so_line': 15066},\n",
       " 'cpi_mintegral': {'account_id': 'RA0000000330',\n",
       "  'account_name': 'Mintegral International Limited',\n",
       "  'so_header': 10010048,\n",
       "  'so_line': 15084},\n",
       " 'cpi_mobvista': {'account_id': 'RA0000000330',\n",
       "  'account_name': 'Mobvista',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'cpm_mintegral': {'account_id': 'DA0000000017',\n",
       "  'account_name': '聚移国际有限公司',\n",
       "  'so_header': 10010062,\n",
       "  'so_line': 15098},\n",
       " 'cpm_mobvista': {'account_id': 'DA0000000017',\n",
       "  'account_name': '聚移国际有限公司',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'mopub': {'account_id': 'DA0000000023',\n",
       "  'account_name': 'TwitterInc.',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'mytarget': {'account_id': 'DA0000000016',\n",
       "  'account_name': 'MGL MY.COM (CYPRUS) LIMITED',\n",
       "  'so_header': 10010074,\n",
       "  'so_line': 15110},\n",
       " 'pubnative': {'account_id': 'DA0000000005',\n",
       "  'account_name': 'Pubnative GmbH',\n",
       "  'so_header': 10010068,\n",
       "  'so_line': 15104},\n",
       " 'smaato': {'account_id': 'DA0000000006',\n",
       "  'account_name': 'Smaato Inc.',\n",
       "  'so_header': 10010067,\n",
       "  'so_line': 15103},\n",
       " 'smadex': {'account_id': 'DA0000000012',\n",
       "  'account_name': 'SMADEX, S.L.U.',\n",
       "  'so_header': 10010071,\n",
       "  'so_line': 15107},\n",
       " 'startapp': {'account_id': 'DA0000000009',\n",
       "  'account_name': 'StartAppInc.',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'telecoming': {'account_id': 'DA0000000010',\n",
       "  'account_name': 'Telecoming, S.A.',\n",
       "  'so_header': 10010072,\n",
       "  'so_line': 15108},\n",
       " 'cpi_xapads': {'account_id': 'DA0000000065',\n",
       "  'account_name': 'Xapads Media Pvt Ltd',\n",
       "  'so_header': 10010037,\n",
       "  'so_line': 15073},\n",
       " 'cpm_xapads': {'account_id': 'DA0000000007',\n",
       "  'account_name': 'Xapads Media Pvt Ltd',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'flymobi': {'account_id': 'DA0000000064',\n",
       "  'account_name': 'Flymobi Technology Group Limited',\n",
       "  'so_header': 10010044,\n",
       "  'so_line': 15080},\n",
       " 'yandex': {'account_id': 'DA0000000011',\n",
       "  'account_name': 'Yandex Europe AG',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'yeahmobi_online': {'account_id': 'DA0000000021',\n",
       "  'account_name': 'Click Tech Limited',\n",
       "  'so_header': 10010075,\n",
       "  'so_line': 15111},\n",
       " 'yeahmobi': {'account_id': 'DA0000000001',\n",
       "  'account_name': 'Click Tech Limited',\n",
       "  'so_header': 10010075,\n",
       "  'so_line': 15111},\n",
       " 'appnext': {'account_id': 'RA0000000328',\n",
       "  'account_name': 'APPNEXT PTE.LTD.',\n",
       "  'so_header': 10010047,\n",
       "  'so_line': 15083},\n",
       " 'affise': {'account_id': 'DA0000000026',\n",
       "  'account_name': 'Affise',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'affle': {'account_id': 'DA0000000022',\n",
       "  'account_name': 'Affle International Pte. Ltd.',\n",
       "  'so_header': 10010022,\n",
       "  'so_line': 15058},\n",
       " 'mobupps': {'account_id': 'DA0000000033',\n",
       "  'account_name': 'MobUpps International Ltd.',\n",
       "  'so_header': 10010046,\n",
       "  'so_line': 15082},\n",
       " 'mobisummer': {'account_id': 'DA0000000034',\n",
       "  'account_name': 'TECDO HONG KONG LIMITED',\n",
       "  'so_header': 10010049,\n",
       "  'so_line': 15085},\n",
       " 'Apple': {'account_id': 'DA0000000050',\n",
       "  'account_name': 'Apple Store',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'adsense': {'account_id': 'DA0000000063',\n",
       "  'account_name': 'Google Asia Pacific Pte. Ltd.,-Adsesne',\n",
       "  'so_header': 10010060,\n",
       "  'so_line': 15096},\n",
       " 'affinity': {'account_id': 'DA0000000041',\n",
       "  'account_name': 'Affinity Global In',\n",
       "  'so_header': 10010050,\n",
       "  'so_line': 15086},\n",
       " 'flatads': {'account_id': 'DA0000000042',\n",
       "  'account_name': 'FLAT MEDIA GLOBAL PTE. LTD.',\n",
       "  'so_header': 10010034,\n",
       "  'so_line': 15070},\n",
       " 'nativex': {'account_id': 'DA0000000055',\n",
       "  'account_name': 'Mobvista International Technology Limited',\n",
       "  'so_header': 10010051,\n",
       "  'so_line': 15087},\n",
       " 'xiaomi': {'account_id': 'DA0000000057',\n",
       "  'account_name': 'Guangzhou Xiaomi information Service Co.,Ltd.',\n",
       "  'so_header': 10010052,\n",
       "  'so_line': 15088},\n",
       " 'xinzhan': {'account_id': 'DA0000000054',\n",
       "  'account_name': 'xinzhan-DSP_CPI',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'avow': {'account_id': 'RA0000000528',\n",
       "  'account_name': 'AVOW GmbH',\n",
       "  'so_header': 10010021,\n",
       "  'so_line': 15057},\n",
       " 'mobavenue': {'account_id': 'DA0000000045',\n",
       "  'account_name': 'MobAvenue Pte Ltd.',\n",
       "  'so_header': 10010033,\n",
       "  'so_line': 15069},\n",
       " 'vfine': {'account_id': 'DA0000000047',\n",
       "  'account_name': 'Vfine Info Technology Co., Lomited',\n",
       "  'so_header': 10010032,\n",
       "  'so_line': 15068},\n",
       " 'cchange': {'account_id': 'DA0000000058',\n",
       "  'account_name': 'CCHANGE TECHNOLOGY LIMITED',\n",
       "  'so_header': 10010040,\n",
       "  'so_line': 15076},\n",
       " 'applovin': {'account_id': 'DA0000000076',\n",
       "  'account_name': 'AppLovin',\n",
       "  'so_header': 10010066,\n",
       "  'so_line': 15102},\n",
       " 'pangle': {'account_id': 'DA0000000071',\n",
       "  'account_name': 'BYTEDANCE PTE. LTD',\n",
       "  'so_header': 10010065,\n",
       "  'so_line': 15101},\n",
       " 'bigo': {'account_id': 'DA0000000075',\n",
       "  'account_name': 'BIGO TECHNOLOGY PTE.LTD',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0},\n",
       " 'RTBhouse': {'account_id': 'DA0000000089',\n",
       "  'account_name': 'RTB MARKETING & TECH SERVICES LIMITED',\n",
       "  'so_header': 0,\n",
       "  'so_line': 0}}"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "1360c97f",
   "metadata": {},
   "outputs": [],
   "source": [
    "src_pt='cpm_adx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "29d7ba8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'adx'"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "src_pt[4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f768fdf6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
